﻿using SuperX.Common.Abstract;
using SuperX.Common;
using SuperX.DataService;
using SuperX.DataService.Abstract;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Net;
using SuperX.Common.Object;
using SuperX.Helper;
using SuperX.Common.BasicService;
using SuperX.Common.Collection;
using Newtonsoft.Json;
using System.IO;
using System.Text;
using SuperX.Common.Log;

namespace SuperX.DataService
{
    public abstract class FileDriverBase : DriverBase, IScanFileDriver
    {
        private string _fileName;

        public string FileName
        {
            get
            {
                return _fileName;
            }
            set
            {
                _fileName = value;
            }
        }

        private string _inLno;

        /// <summary>
        /// 线体编码
        /// </summary>
        public string InLno
        {
            get { return _inLno; }
            set { _inLno = value; }
        }

        private string _no;

        /// <summary>
        /// AB机
        /// </summary>
        public string NO
        {
            get { return _no; }
            set { _no = value; }
        }

        private string _Factory;

        /// <summary>
        ///
        /// </summary>
        public string Factory
        {
            get { return _Factory; }
            set { _Factory = value; }
        }

        public List<FileAnalysisConfig> FileAnalysis { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override IBasicObject this[string name] => throw new NotImplementedException();

        protected IDataServer _parent;

        public FileDriverBase(IDataServer parent, string name)
        {
            Name = name;
            _parent = parent;
            this.Parent = parent;
            _server = parent;
        }

        public abstract bool Init();

        public override IGroup AddGroup(string name, int updateRate, float deadBand = 0, bool active = false)
        {
            FileDeviceGroup group = new FileDeviceGroup(name, updateRate, active, this);
            DriverGroup.Add(group);
            return group;
        }

        public override bool Connect()
        {
            foreach (var item in DriverGroup)
            {
                item.IsActive = true;
            }
            return true;
        }

        public override void Dispose()
        {
            foreach (var group in DriverGroup)
            {
                group.Dispose();
            }
            DriverGroup.Clear();
            base.Dispose();
        }

        #region 驱动默认读写方法

        public byte[] ReadBytes(DeviceAddress address, ushort size)
        {
            return null;
        }

        public ItemData<int> ReadInt32(DeviceAddress address)
        {
            return new ItemData<int>(0, 0, QUALITIES.OK);
        }

        public ItemData<uint> ReadUInt32(DeviceAddress address)
        {
            return new ItemData<uint>(0, 0, QUALITIES.OK);
        }

        public ItemData<ushort> ReadUInt16(DeviceAddress address)
        {
            return new ItemData<ushort>(0, 0, QUALITIES.OK);
        }

        public ItemData<short> ReadInt16(DeviceAddress address)
        {
            return new ItemData<short>(0, 0, QUALITIES.OK);
        }

        public ItemData<byte> ReadByte(DeviceAddress address)
        {
            return new ItemData<byte>(0, 0, QUALITIES.OK);
        }

        public ItemData<string> ReadString(DeviceAddress address, ushort size)
        {
            return new ItemData<string>("", 0, QUALITIES.OK);
        }

        public ItemData<float> ReadFloat(DeviceAddress address)
        {
            return new ItemData<float>(0, 0, QUALITIES.OK);
        }

        public ItemData<bool> ReadBit(DeviceAddress address)
        {
            return new ItemData<bool>(false, 0, QUALITIES.OK);
        }

        public override ItemData<dynamic> ReadDynamic(DeviceAddress address)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex);
            return tag == null ? new ItemData<dynamic>(false, 0, QUALITIES.NG) : new ItemData<dynamic>(tag.Value, 0, QUALITIES.OK);
        }

        public int WriteBytes(DeviceAddress address, byte[] bit)
        {
            return 0;
        }

        public virtual int WriteBits(DeviceAddress address, byte bits)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as ByteTag;
            if (tag != null)
            {
                return tag.Write(bits);
            }
            return -1;
        }

        public int WriteInt16(DeviceAddress address, short value)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as ShortTag;
            if (tag != null)
            {
                return tag.Write(value);
            }
            return -1;
        }

        public int WriteUInt16(DeviceAddress address, ushort value)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as UShortTag;
            if (tag != null)
            {
                return tag.Write(value);
            }
            return -1;
        }

        public int WriteUInt32(DeviceAddress address, uint value)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as UIntTag;
            if (tag != null)
            {
                return tag.Write(value);
            }
            return -1;
        }

        public int WriteInt32(DeviceAddress address, int value)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as IntTag;
            if (tag != null)
            {
                return tag.Write(value);
            }
            return -1;
        }

        public int WriteFloat(DeviceAddress address, float value)
        {
            var tag = _parent.IndexFind((short)address.CacheIndex) as FloatTag;
            if (tag != null)
            {
                return tag.Write(value);
            }
            return -1;
        }

        public int WriteValue(DeviceAddress address, object value)
        {
            return 0;
        }

        public int WriteString(DeviceAddress address, string str)
        {
            return 0;
        }

        public int WriteBit(DeviceAddress address, bool bit)
        {
            return 0;
        }

        #endregion 驱动默认读写方法

        public abstract FileData[] ReadAll(string groupName);

        public abstract void CreateDefaultAddresses();

        public abstract List<FileData[]> ReadToList(string groupName);

        public string GetLogPath()
        {
            return null;
        }

        public void Scan()
        {
            throw new NotImplementedException();
        }

        public bool[] ReadBools(DeviceAddress address, ushort size)
        {
            throw new NotImplementedException();
        }

        public void UpdateIOTag(string groupName)
        {
            throw new NotImplementedException();
        }

        public int WriteDynamic(IIOTag tag, object value)
        {
            throw new NotImplementedException();
        }

        protected List<string> LogBuffer = new List<string>();
        protected long OffsetPostion;
    }
}